var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,$ = layui.jquery,laydate = layui.laydate;

function getAllJson(jsons) {
    let tmpitem=[]
    for (let item of jsons) {
        let tmparr={}
        tmparr["value"]=item["code"];
        tmparr["name"]=item["name"];
        if(item["users"]){
            for(let it of item["users"]){
                item["list"].push(it)
            }
        }
        if(item["list"] && item["list"].length){
            tmparr["children"]=   this.getAllJson(item["list"]);
            tmpitem.push(tmparr)
        }else{
            tmpitem.push(tmparr)
        }

    }
    return tmpitem
}

function getxmselect(el,name, data) {
    return xmSelect.render({
        el: el,
        name: name,

        tree: {
            show: true,
            showLine: true,
            showFolderIcon:true,
            indent: 20,
            expandedKeys:false
        },
        height: 'auto',
        data: data,
        on: function ({ arr, change, isAdd }) {
            return this
        }
    })
}
layui.use(['tableEdit',"excel"], function () {
    let tableEdit = layui.tableEdit;
    let layer = layui.layer,
        excel = layui.excel;

    lay('.starttime').each(function(){
        laydate.render({
            elem: this
        });
    });

    let deptSalesmanList=[];
    layer.ready(function () {
        //获取当前用户所属部门树()
        let currentDeptTreeList = getAllUserDepartmentTree();
        if (currentDeptTreeList && currentDeptTreeList.list) {
            let deptTree = getAllJson(currentDeptTreeList.list);
            deptSalesmanList = getxmselect("#deptSalesmanCode", "deptSalesmanCode", deptTree);
        } else {
            deptSalesmanList = getxmselect("#deptSalesmanCode", "deptSalesmanCode", [{name: user.name, value: user.code}]);
        }

    });
    form.render();//表单刷新
    let initData = [];
    table.render({
        elem: '#sellOutStock1Select'
        ,data:initData
        ,toolbar: '#sellOutStock1Toolbar'
        ,totalRow: true
        ,cols: [[
            {field: 'numbers',width: 50,type:'numbers',title:'序号', totalRowText: '合计'}
            ,{field:'billNo', title:'出库单号',sort:true, align: 'center', width: 120}
            ,{field:'timeStr', title:'出库日期',sort:true, align: 'center', width: 120}
            ,{field:'carryPlanNo', title:'出运计划号',sort:true, align: 'center', width: 120}
            ,{field:'customerName', title:'单位名称',sort:true, align: 'center', width: 120}
            ,{field:'outPerson', title:'出库人',sort:true, align: 'center', width: 120}
            ,{field:'createDateStr', title:'创建时间',sort:true, align: 'center', width: 120}
            ,{field:'pickUpDateStr', title:'提货日期',sort:true, align: 'center', width: 120},
            {field: 'productCode', width: 120, title: '产品编码', sort: true, align: 'center'},
            {field: 'specificationCode', width: 120, title: '规格编码', sort: true, align: 'center'},
            {field: 'saleContractNo', width: 120, title: '销售合同号', sort: true, align: 'center'},
            {field: 'contractSalesmanName', title: '销售业务员', sort: true, width: 200, align: 'center'},
            {field: 'clientContractNo', width: 120, title: '客户合同号', sort: true, align: 'center'},
            {field: 'productUnitName', width: 120, title: '产品单位', sort: true, align: 'center'},
            {field: 'productCname', width: 120, title: '中文名称', sort: true, align: 'center'},
            {field: 'productEname', width: 120, title: '英文名称', sort: true, align: 'center'},
            {field: 'productCdesc', width: 120, title: '中文描述', sort: true, align: 'center'},
            {field: 'productEdesc', width: 120, title: '英文描述', sort: true, align: 'center'},
            {field: 'specificationExplanation', width: 120, title: '规格说明', sort: true, align: 'center'},
            {field: 'lotNo', width: 120, title: '批次号', sort: true, align: 'center'},
            {field: 'outStockQuantity', width: 120, title: '出库数量', sort: true, align: 'center', totalRow: true},

            {field: 'fcUnitPrice', width: 120, title: '销售单价', sort: true, align: 'center'},
            {field: 'moneySubtotal', width: 120, title: '金额小计', sort: true, align: 'center'},
            {field: 'outStockCost', width: 120, title: '出库成本', sort: true, align: 'center'},
            {field: 'costSubtotal', width: 120, title: '成本小计', sort: true, align: 'center', totalRow: true},
            {field: 'productUnitPrice', width: 120, title: '出库产品成本', sort: true, align: 'center'},

            // {field: 'boxAmount', width: 80, title: '箱数', sort: true, align: 'center', totalRow: true},
            // {field: 'everyBoxAmount', width: 120, title: '每箱数量', sort: true, align: 'center', totalRow: true},
            // {field: 'trayNumber', width: 120, title: '托盘入数', sort: true, align: 'center', totalRow: true},
            // {field: 'totalTray', width: 120, title: '总托盘', sort: true, align: 'center', totalRow: true},
        ]]
        ,id: 'maininformationtableid'
        ,height: $(document).height() - $('#sellOutStock1Select').offset().top - 75,
        //开启分页
        page: true,
        limits: [300,600,900],
    });


//搜索提交
    form.on('submit(submitform)', function(data){
        table.reload('maininformationtableid', {
            page: {
                limit:300,
                curr: 1
            },
            url:'sellOutStock/sellOutStockStatistics1ListByPage',
            where:  data.field
        }, 'data');
        return false;
    });


    form.on('submit(exportData)', function (queryReq) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: queryReq.field,
            url:"sellOutStock/sellOutStockStatistics1Export",
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 80,
                        'X': 80
                    }, 80);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '销售出库产品列表统计.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });


    document.getElementById('clear').onclick = function(){
        deptSalesmanList.setValue([ ]);
    };

});
